//
//  YWMeterReadingRecordTableViewCell.m
//  maintain
//
//  Created by Kyo on 16/9/24.
//  Copyright © 2016年 ewin. All rights reserved.
//

#import "YWMeterReadingRecordTableViewCell.h"
#import "UIColor+CustomColors.h"
#import "YWCommonTool.h"
#import "YWMacro.h"
//#import "UILabel+AttributedText.h"
#import "YYLabel.h"

#import "YWBuildingManager.h"
#import "YWEquipment.h"
#import "YWEquipmentManager.h"
#import "YWMeterReadingRecord.h"
#import "YWMeterReadingManager.h"

@interface YWMeterReadingRecordTableViewCell ()
@property (nonatomic, strong) YYLabel* executorLabel;
@property (nonatomic, strong) YYLabel* typeLabel;
@property (nonatomic, strong) YYLabel* readDate;
@property (nonatomic, strong) YYLabel* locationName;
@property (nonatomic, strong) YYLabel* thisReadNumer;
@end
@implementation YWMeterReadingRecordTableViewCell

+ (instancetype)cellWithTableView:(UITableView*)tableView
{
    static NSString* identifier = @"YWMeterReadingRecordTableViewCellIdentifier";
    YWMeterReadingRecordTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[YWMeterReadingRecordTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleDefault;
    }
    return cell;
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString*)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
    }
    return self;
}
- (YYLabel *)executorLabel{
    if (!_executorLabel) {
        YYLabel* executorLabel = [[YYLabel alloc] init];
        executorLabel.font = kContentFont;
        [self.contentView addSubview:executorLabel];
        _executorLabel = executorLabel;
    }
    return _executorLabel;
}

- (YYLabel *)typeLabel{
    if (!_typeLabel) {
        YYLabel* label = [[YYLabel alloc] init];
        label.font = kContentFont;
        label.textColor = [UIColor customTextGrayColor];
        label.textAlignment = NSTextAlignmentRight;
        [self.contentView addSubview:label];
        _typeLabel = label;
    }
    return _typeLabel;
}

- (YYLabel*)locationName
{
    if (!_locationName) {
        YYLabel* locationName = [[YYLabel alloc] init];
        locationName.font = kContentFont;
        [self.contentView addSubview:locationName];
        _locationName = locationName;
    }
    return _locationName;
}

- (YYLabel*)readDate
{
    if (!_readDate) {
        YYLabel* readDate = [[YYLabel alloc] init];
        readDate.font = kContentFont;
        [self.contentView addSubview:readDate];
        _readDate = readDate;
    }
    return _readDate;
}

- (YYLabel*)thisReadNumer
{
    if (!_thisReadNumer) {
        YYLabel* thisReadNumer = [[YYLabel alloc] init];
        thisReadNumer.font = kContentFont;
        [self.contentView addSubview:thisReadNumer];
        _thisReadNumer = thisReadNumer;
    }
    return _thisReadNumer;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    CGFloat gap = 12.0;
    CGFloat width = SCREEN_WIDTH - gap*2;
    CGFloat labelH = 23.0;
    CGFloat typeLabelW = 50;
    self.executorLabel.frame = CGRectMake(gap, gap, width-typeLabelW-gap, labelH);
    
    self.typeLabel.frame = CGRectMake(CGRectGetMaxX(self.executorLabel.frame)+gap, gap, typeLabelW, labelH);
    
    self.readDate.frame = CGRectMake(gap, CGRectGetMaxY(self.executorLabel.frame), width, labelH);
    
    self.locationName.frame = CGRectMake(gap, CGRectGetMaxY(self.readDate.frame), width, labelH);
    
    self.thisReadNumer.frame = CGRectMake(gap, CGRectGetMaxY(self.locationName.frame), width, labelH);
}

- (void)setMeterReadingRecord:(YWMeterReadingRecord*)meterReadingRecord
{
    _meterReadingRecord = meterReadingRecord;
    YWEquipment* equipment = [[YWEquipmentManager shareManager] equipmentWithId:meterReadingRecord.equipment.equipmentId];
    
    self.executorLabel.text = [NSString stringWithFormat:@"抄表人: %@",meterReadingRecord.creator.userName?:@"无"];
    
    if (equipment.equipmentTypeId == [[YWMeterReadingManager shareManager] waterMeterEquipmentTypeId]) {
        self.typeLabel.text = @"水表";
    }else if (equipment.equipmentTypeId == [[YWMeterReadingManager shareManager] electricMeterEquipmentTypeId]){
        self.typeLabel.text = @"电表";
    }else if (equipment.equipmentTypeId == [[YWMeterReadingManager shareManager] gasMeterEquipmentTypeId]){
        self.typeLabel.text = @"燃气表";
    }
    
    NSString *time = [YWCommonTool showTimeWithNormalFormatter:meterReadingRecord.readingTime/1000];
    
//    [self.readDate labelText:@"抄表时间: " withAttributedText:time.length?time:@"无" andAttributedColor:[UIColor customYellowColor]];
    self.readDate.attributedText = [YWCommonTool attributedTextWithFirstText:@"抄表时间: " firstColor:[UIColor blackColor] secondText:time.length?time:@"无" secondColor:[UIColor customYellowColor] font:kContentFont];
    
    NSString *location = [[YWBuildingManager shareManager] equipmentLocation:equipment];
    self.locationName.text = [NSString stringWithFormat:@"位置: %@",location.length?location:@"无"];
    NSString *thisReadStr = meterReadingRecord.usedAmount>0?[NSString stringWithFormat:@"%.1f",meterReadingRecord.usedAmount]:@"无";
    //[self.thisReadNumer labelText:@"本次用量: " withAttributedText:thisReadStr.length?thisReadStr:@"无" andAttributedColor:[UIColor customYellowColor]];
    self.thisReadNumer.attributedText = [YWCommonTool attributedTextWithFirstText:@"本次用量: " firstColor:[UIColor blackColor] secondText:thisReadStr.length?thisReadStr:@"无" secondColor:[UIColor customYellowColor] font:kContentFont];
}

@end
